package com.example.demo.controller;

import org.apache.tomcat.util.buf.HexUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

public class AESUtil {

    //AES加密数据  pass为24位密钥字符串，bb=s.getBytes("UTF-8")原文；返回：经BASE64编码的密文
    public static String addAES(String k,String s){
        String re="";
        try{
            KeyGenerator kgen=KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(k.getBytes());
            kgen.init(128, secureRandom);
            SecretKeySpec key=new SecretKeySpec(kgen.generateKey().getEncoded(),"AES");
            Cipher cipher=Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] bytes = cipher.doFinal(s.getBytes("UTF-8"));
            System.out.println(HexUtils.toHexString(bytes));
            re=addBASE64(bytes);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return re;
    }

    public static String addAES(String k,byte[] bb){
        String re="";
        try{
            KeyGenerator kgen=KeyGenerator.getInstance("AES");
            kgen.init(128,new SecureRandom(k.getBytes()));
            SecretKeySpec key=new SecretKeySpec(kgen.generateKey().getEncoded(),"AES");
            Cipher cipher=Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] bytes = cipher.doFinal(bb);
            System.out.println(HexUtils.toHexString(bytes));
            re=addBASE64(bytes);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return re;
    }
    //AES解密数据   pass为24位密钥字符串，b64为经BASE64编码的密文；返回：原文
    public static String delAES(String k,String b64){
        String re="";
        try{
            KeyGenerator kgen=KeyGenerator.getInstance("AES");
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(k.getBytes());
            kgen.init(128, secureRandom);
            SecretKeySpec key=new SecretKeySpec(kgen.generateKey().getEncoded(),"AES");
            Cipher cipher=Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            re=new String(cipher.doFinal(delBASE64(b64)),"UTF-8");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return re;
    }

    //BASE64编码,返回无TAB、空格、\r、\n
    public static String addBASE64(byte[] bb){
        String re="";
        try{
            re=new BASE64Encoder().encode(bb);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return re.replaceAll("\t","").replaceAll(" ","").replaceAll("\r","").replaceAll("\n","");
    }
    //BASE64解码
    public static byte[] delBASE64(String s){
        byte[] re=new byte[0];
        try{
            re=new BASE64Decoder().decodeBuffer(s);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return re;
    }

    public static void main(String[] args) {
        String key="123456789012345678901234";
        String plaintext="1234567890中华人民共和国abcdefghijklmnopqrstuvwxyz";
        String ciphertext=addAES(key,plaintext);
        System.out.println("ciphertext:"+ciphertext);






    }
}
